package Leetcode;

public class problem29 {
    public static int divide(int dividend, int divisor) {
        if(divisor == 0 || (dividend == Integer.MIN_VALUE && divisor == -1)){
            return Integer.MAX_VALUE;
        }
        long m = Math.abs((long)dividend);
        long n = Math.abs((long)divisor);
        long res = 0;

        int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1;

        if(n == 1){
            return (int) (sign == -1 ? -m : m);
        }

        while (m >= n){
            long t = n;
            long p = 1;
            while(m >= (t << 1)){
                t <<=1;
                p <<=1;
            }
            res += p;
            m -= t;
        }

        return (int) (sign == -1 ? -m : m);

    }

    public static void main(String[] args) {
        divide(-2147483648,2);
    }

}
